package isssr.dao.test;

import isssr.dao.exception.DAOException;
import isssr.dao.factory.DAOFactory;
import isssr.dao.interfaces.UserDAO;
import isssr.entity.src.Role;
import isssr.entity.src.User;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.mbeans.UserMBean;

public class UserTestBuilder {

	private static Integer sequenceCounter = 0;
	

	private Integer getNextSequenceNumber()
	{
		return  sequenceCounter++;
	}
	
	
	private List<User> registeredUsers;
	private RoleTestBuilder roleBuilder;
	
	public UserTestBuilder()
	{
		this.roleBuilder = new RoleTestBuilder();
		this.registeredUsers = new ArrayList<User>();
	}
	

	private String buildStringField(String prefix)
	{
		return prefix + "_" + getNextSequenceNumber();
	}
	
	/*
	private String buildHashedStringField(String prefix) 
	{
		String stringField = this.buildStringField(prefix);
		return this.hashString(stringField);
	}
	
	
	private String hashString(String toHash)
	{
		String ret = null;
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			
			e.printStackTrace();
			return ret;
		}
        md.update(toHash.getBytes());
 
        byte byteData[] = md.digest();
 
        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
		
        ret = sb.toString();
		return ret;		
	}
	*/
	
	
	private User createNewUser() throws DAOException
	{
		String username = this.buildStringField("username");
		Role role = this.roleBuilder.generateRole();
		DAOFactory.getDAOFactory().getRoleDAO().insert(role);
		
		return new User(username, role);
	}
	
	
	public User generateUser() throws DAOException
	{
		User user = createNewUser();
		this.registeredUsers.add(user);
		
		return user;
	}
	
	public User generateUser(String username, Role role)
	{
		User user;
		if(role == null)
			user = new User(username, roleBuilder.generateRole());
		else
			user = new User(username, role);
		this.registeredUsers.add(user);
		
		return user;
	}
	
	public User generateUser(String username, Integer level) throws DAOException
	{
		Role role = this.roleBuilder.generateRole(level);
		DAOFactory.getDAOFactory().getRoleDAO().insert(role);
		return generateUser(username, role);
		
	}
	
	public User generateUser(String username)
	{
		return generateUser(username, (Role) null);		
	}

	public User generateUser(Role role) throws DAOException
	{
		if(role == null)
			return generateUser();
		
		User user = new User("username_" + getNextSequenceNumber(), role);
		this.registeredUsers.add(user);
		
		return user;
	}
	
	public void unregisterUser(User user)
	{
		this.registeredUsers.remove(user);
	}
	
	public void cleanUp()
	{
		UserDAO userDAO = DAOFactory.getDAOFactory().getUserDAO();
		
		for(User user : this.registeredUsers)
		{
			try
			{
				userDAO.delete(user);
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
		
		this.registeredUsers.clear();
		this.roleBuilder.cleanUp();
	}
	
}
